package com.example.governmentcrawler.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.governmentcrawler.entity.Contract.ProcurementForContract;
import com.example.governmentcrawler.entity.Gov.ProcurementForGov;
import com.example.governmentcrawler.entity.GreenPur.ProcurementForGreenPur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @projectName: GovernmentCrawler
 * @package: com.example.governmentcrawler.utils
 * @className: GetDetailIdForGov
 * @author: xxXPCSC
 * @description: TODO
 * @date: 2024/2/26 17:09
 * @version: 1.0
 **/
@Component
public class GetDetailId {
    @Autowired
    private SendHttp sendHttp;

    public JSONObject sendPostForGov(String url,String keyword,String keycode,int day,int pageNo){
        //日期选择
        LocalDate currentDate = LocalDate.now();
        String beginDay;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if(day==1) {
            LocalDate twoMonthsAgoDate = currentDate.minusDays(14);
            beginDay = twoMonthsAgoDate.format(formatter);
        }else{
            LocalDate yesterday = currentDate.minusDays(1);
            beginDay = yesterday.format(formatter);
        }
        String endDay = currentDate.format(formatter);


        //插入请求体
        ProcurementForGov procurementForGov = new ProcurementForGov();
        procurementForGov.setPublishDateBegin(beginDay);
        procurementForGov.setPublishDateEnd(endDay);
        procurementForGov.setPageNo(pageNo);
        procurementForGov.setKeyword(keyword);
        procurementForGov.setCategoryCode(keycode);
        procurementForGov.setPageSize(100);

        String jsonString = JSON.toJSONString(procurementForGov);
//        System.out.println(url+jsonString);
        try {
            JSONObject jsonObject = sendHttp.sendHTTPPost(url, jsonString, "application/json;charset=UTF-8");
            return jsonObject;
        }catch (Exception e){
            return null;
        }
    }

    public JSONObject sendPostForContract(String url,String keyword,String keycode){
        ProcurementForContract procurementForContract = new ProcurementForContract();
        procurementForContract.setContent(keyword);
        procurementForContract.setCategoryNum(keycode);

        String jsonString = JSON.toJSONString(procurementForContract);
        String data ="params="+jsonString;
        try{
            return sendHttp.sendHTTPPost(url,data,"application/x-www-form-urlencoded; charset=UTF-8");
        }catch (Exception e){
            System.out.println(e.toString());
            return null;
        }

    }

    public JSONObject sendPostForGreenPur(String url,String keyword,String keycode,int pageSize){
        ProcurementForGreenPur procurementForGreenPur = new ProcurementForGreenPur();
        //转换为 url编码
        String encode = URLEncoder.encode(keycode);

        String s = "siteGuid="+procurementForGreenPur.getSiteGuid()+"&cateNum="+encode+"&content="+keyword+"&xiaqucode="+
                "&pageIndex=" +0+
                "&pageSize="+pageSize;
        try{
            return sendHttp.sendHTTPPost(url,s,"application/x-www-form-urlencoded; charset=UTF-8");
        }catch (Exception e){
            System.out.println(e.toString());
            return null;
        }
    }

    public JSONObject sendPostForPublicRe(String url,String keycode,int pagesize,int pageno){
        String s = "pagesize="+pagesize+"&channelid="+keycode+"&pageno="+pageno;
        try{
            return sendHttp.sendHTTPPost(url+s,s,"application/x-www-form-urlencoded; charset=UTF-8");
        }catch (Exception e){
            System.out.println(e.toString());
            return null;
        }
    }

    public JSONObject sendPostForPublicReHz(String url,int cid){
        String s = "cid="+cid+"&siteid=656&pagesize=120&pageno=1";
        try{
            return sendHttp.sendHTTPPostForPublicHz(url+s,s,"application/x-www-form-urlencoded; charset=UTF-8");
        }catch (Exception e){
            System.out.println(e.toString());
            return null;
        }
    }

    public String sendPostForOnLineMarket(String url,String keyword,int currentJDBCPage){
        String encodeLocation = URLEncoder.encode("湖州市");
        String encodeState = URLEncoder.encode("已经发布");
        String encodeKeyWord= URLEncoder.encode(keyword);
        String s="sunit_name="+encodeLocation+"&mode="+encodeState+"&sproject_name="+encodeKeyWord+"&currentJDBCPage="+currentJDBCPage;
        try{
            return sendHttp.sendHTTPPostForOnline(url+s,s,keyword);
        }catch (Exception e){
            System.out.println(e.toString());
            return null;
        }

    }
}
